package com.example.kermit.zhpaper.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.example.kermit.zhpaper.R;
import com.example.kermit.zhpaper.config.MyAppConfig;
import com.example.kermit.zhpaper.entity.SplashPic;
import com.example.kermit.zhpaper.http.MySingleRequestQueue;
import com.example.kermit.zhpaper.ui.base.BaseActivity;
import com.google.gson.Gson;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Kermit on 15-7-25.
 * e-mail : wk19951231@163.com
 */
public class SplashActivity extends BaseActivity {

    private final int SPLASH_DISPLAY_TIME = 5000;

    @Bind(R.id.splash_img)
    NetworkImageView splash_img;

    MySingleRequestQueue mySingleRequestQueue;
    ImageLoader imageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity_layout);
        ButterKnife.bind(this);

        imageLoader = new MySingleRequestQueue(this).getImageLoader();
        StringRequest stringRequest = new StringRequest(MyAppConfig.SPLASH_IMG,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(@NonNull String response) {
                        Gson gson = new Gson();
                        SplashPic pic = gson.fromJson(response, SplashPic.class);
                        String picUrl = pic.getImg();
                        Log.i("kermit", picUrl);

                        splash_img.setImageUrl(picUrl, imageLoader);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                                startActivity(intent);
                                SplashActivity.this.finish();
                            }
                        }, SPLASH_DISPLAY_TIME);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );
        mySingleRequestQueue = MySingleRequestQueue.getInstance(this);
        mySingleRequestQueue.addtoRequestQueue(stringRequest);
    }
}
